package com.hp.system.service.impl;

import com.hp.common.constant.UserConstants;
import com.hp.common.core.text.Convert;
import com.hp.common.exception.BusinessException;
import com.hp.system.domain.DestbationCity;
import com.hp.system.domain.DestbationCityExample;
import com.hp.system.mapper.DestbationCityMapper;
import com.hp.system.service.DestbationCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DestbationCityServiceImpl implements DestbationCityService {

    @Autowired(required = false)
    private DestbationCityMapper destbationCityMapper;


    @Override
    public int deleteCityByIds(String ids)  {
        return destbationCityMapper.deleteCity(Convert.toStrArray(ids));
    }


    @Override
    public List<DestbationCity> showCity() {
        DestbationCityExample destbationCityExample = new DestbationCityExample();
        DestbationCityExample.Criteria criteria = destbationCityExample.createCriteria();
        criteria.andIsdeleEqualTo(new Byte("2"));
        return  destbationCityMapper.selectByExample(destbationCityExample);
    }

    @Override
    public List<DestbationCity> showDestCity(Integer destId) {
        DestbationCityExample destbationCityExample = new DestbationCityExample();
        DestbationCityExample.Criteria criteria = destbationCityExample.createCriteria();
        criteria.andIsdeleEqualTo(new Byte("2"));
        criteria.andDestbationidEqualTo(destId);
        return  destbationCityMapper.selectByExample(destbationCityExample);
    }

    @Override
    public Integer getId(Integer destbation, Integer city) {
        DestbationCityExample destbationCityExample = new DestbationCityExample();
        DestbationCityExample.Criteria criteria = destbationCityExample.createCriteria();
        criteria.andIsdeleEqualTo(new Byte("2"));
        criteria.andDestbationidEqualTo(destbation);
        criteria.andCityidEqualTo(city);
        List<DestbationCity> list = destbationCityMapper.selectByExample(destbationCityExample);
        DestbationCity destbationCity = list.get(0);
        return destbationCity.getId();
    }

    @Override
    public List<DestbationCity> selectByDest(DestbationCity destbationCity) {
        return destbationCityMapper.showDestCity(destbationCity);
    }

    @Override
    public String checkCityNameUnique(DestbationCity destbationCity) {
        String newcity = destbationCity.getCityname();
        List<String> list = destbationCityMapper.getAllCityName();
        for (String city: list
             ) {
            if (city.equals(newcity))
                return UserConstants.CITY_NAME_NOT_UNIQUE;
        }
        return UserConstants.CITY_NAME_UNIQUE;
    }

    @Override
    public int insertCity(DestbationCity destbationCity) {
        Integer cityid = destbationCityMapper.selectCountByid(destbationCity.getDestbationid())+1;
        destbationCity.setCityid(cityid);
        return destbationCityMapper.insertSelective(destbationCity);
    }

    @Override
    public List<DestbationCity> getDestInfoSheet(DestbationCity destbationCity) {
        List<DestbationCity> list = destbationCityMapper.showDestCity(destbationCity);
        return list;
    }

    @Override
    public DestbationCity getDestById(Integer id) {
        return destbationCityMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateDest(DestbationCity destbationCity) {
        return destbationCityMapper.updateByPrimaryKeySelective(destbationCity);
    }
}
